package org.tio.sitexxx.web.server.controller.manager;

import org.redisson.api.RTopic;
import org.tio.core.ChannelContext;
import org.tio.core.maintain.IpStats;
import org.tio.http.common.HttpConfig;
import org.tio.http.common.HttpRequest;
import org.tio.http.server.annotation.RequestPath;
import org.tio.sitexxx.service.model.main.User;
import org.tio.sitexxx.service.model.stat.TioIpPullblackLog;
import org.tio.sitexxx.service.service.base.TioIpPullblackLogService;
import org.tio.sitexxx.service.service.base.UserService;
import org.tio.sitexxx.service.service.conf.IpWhiteListService;
import org.tio.sitexxx.service.init.RedisInit;
import org.tio.sitexxx.service.vo.Const.Topic;
import org.tio.sitexxx.service.vo.topic.CleanViewCacheVo;
import org.tio.sitexxx.web.server.utils.TioIpPullblackUtils;
import org.tio.sitexxx.web.server.utils.WebUtils;
import org.tio.utils.resp.Resp;
import org.tio.utils.ui.layui.table.LayuiPage;

/**
 * @author tanyaowu
 * 2016年6月29日 下午7:53:59
 */
@RequestPath(value = "/m/tio")
public class TioController {
	/**
	 *
	 * @author tanyaowu
	 */
	public TioController() {
	}

	@RequestPath(value = "/clearStaticResCache")
	public Resp clearStaticResCache(HttpRequest request, HttpConfig httpConfig) throws Exception {

		CleanViewCacheVo cleanViewCacheVo = new CleanViewCacheVo();
		RTopic topic = RedisInit.get().getTopic(Topic.CLEAN_VIEW_CACHE);
		topic.publish(cleanViewCacheVo);

		return Resp.ok("缓存已经清空");
	}

	/**
	 * 获取ipStat信息
	 * @param request
	 * @param channelContext
	 * @return
	 * @throws Exception
	 * @author: tanyaowu
	 */
	@RequestPath(value = "/ipStat")
	public Resp ipStat(Long duration, HttpRequest request, ChannelContext channelContext) throws Exception {

		IpStats ipStats = channelContext.groupContext.ipStats;
		//		HttpResponse ret = Resps.json(request, LayuiPage.ok(ipStats.values(duration), ipStats.size(duration)));
		return Resp.ok(LayuiPage.ok(ipStats.values(duration), ipStats.size(duration)));
	}

	/**
	 * 把ip列为白名单
	 * @param ip
	 * @return
	 * @throws Exception
	 */
	@RequestPath(value = "/addWhiteIp")
	public Resp addWhiteIp(HttpRequest request, String ip) throws Exception {
		User currUser = WebUtils.currUser(request);

		String clientip = request.getClientIp();
		Integer curruserid = currUser.getId();
		String remark = curruserid + "," + clientip;
		IpWhiteListService.me.save(ip, remark);
		return Resp.ok();
	}

	/**
	 * 把自己设为白名单
	 * @param request
	 * @return
	 * @throws Exception
	 * @author tanyaowu
	 */
	@RequestPath(value = "/addSelfToWhiteIp")
	public Resp addSelfToWhiteIp(HttpRequest request) throws Exception {
		User currUser = WebUtils.currUser(request);

		String clientip = request.getClientIp();
		Integer curruserid = currUser.getId();
		String remark = "把自己设为白名单, " + curruserid + ", " + clientip;
		IpWhiteListService.me.save(clientip, remark);
		return Resp.ok();
	}

	/**
	 * 将自己从白名单中删除
	 * @param request
	 * @return
	 * @throws Exception
	 * @author tanyaowu
	 */
	@RequestPath(value = "/deleteSelfToWhiteIp")
	public Resp deleteSelfToWhiteIp(HttpRequest request) throws Exception {
		User currUser = WebUtils.currUser(request);

		String clientip = request.getClientIp();
		Integer curruserid = currUser.getId();
		String remark = "将自己从白名单中删除, " + curruserid + ", " + clientip;
		IpWhiteListService.me.delete(clientip, remark);
		return Resp.ok();
	}

	/**
	 * 删除ip白名单
	 * @param request
	 * @param ip
	 * @return
	 * @throws Exception
	 */
	@RequestPath(value = "/deleteWhiteIp")
	public Resp deleteWhiteIp(HttpRequest request, String ip) throws Exception {
		User currUser = WebUtils.currUser(request);

		String clientip = request.getClientIp();
		Integer curruserid = currUser.getId();
		String remark = curruserid + "," + clientip;
		IpWhiteListService.me.delete(ip, remark);
		return Resp.ok();
	}

	/**
	 * 把ip列为黑名单
	 * @param ip
	 * @return
	 * @throws Exception
	 */
	@RequestPath(value = "/addBlackIp")
	public Resp addBlackIp(HttpRequest request, String ip) throws Exception {
		User currUser = WebUtils.currUser(request);

		String clientip = request.getClientIp();
		Integer curruserid = currUser.getId();
		String remark = curruserid + "," + clientip;
		//		TioIpPullblackLogService.ME.addToBlack(ip, request.getChannelContext().getServerNode().getPort(), remark);

		TioIpPullblackUtils.addToBlack(request, remark, TioIpPullblackLog.Type.MANUAL);

		return Resp.ok();
	}

	/**
	 * 删除ip黑名单
	 * @param request
	 * @param ip
	 * @return
	 * @throws Exception
	 */
	@RequestPath(value = "/deleteBlackIp")
	public Resp deleteBlackIp(HttpRequest request, String ip) throws Exception {
		User currUser = WebUtils.currUser(request);

		String clientip = request.getClientIp();
		Integer curruserid = currUser.getId();
		String remark = curruserid + "," + clientip;
		TioIpPullblackLogService.ME.deleteFromBlack(ip, request.getChannelContext().getServerNode().getPort(), remark);
		return Resp.ok();
	}

	/**
	 * 拉黑用户
	 * @param request
	 * @param nick
	 * @return
	 * @throws Exception
	 */
	@RequestPath(value = "/pullBlackUser")
	public Resp pullBlackUser(HttpRequest request, String nick) throws Exception {
		int r = UserService.ME.pullBlackUserByNick(nick);
		if (r > 0) {
			return Resp.ok();
		} else {
			return Resp.fail("拉黑失败，请检查昵称是否正确");
		}
	}

	/**
	 * 把用户的状态设为正常
	 * @param request
	 * @param nick
	 * @return
	 * @throws Exception
	 */
	@RequestPath(value = "/normalUser")
	public Resp normalUser(HttpRequest request, String nick) throws Exception {
		int r = UserService.ME.normalUserByNick(nick);
		if (r > 0) {
			return Resp.ok();
		} else {
			return Resp.fail("操作失败，请检查昵称是否正确");
		}
	}

	/**
	 * 清除所有用户缓存
	 * @param request
	 * @param nick
	 * @return
	 * @throws Exception
	 */
	@RequestPath(value = "/clearAllUserCache")
	public Resp clearAllUserCache(HttpRequest request) throws Exception {
		UserService.ME.clearAllUserCache();
		return Resp.ok();
	}

	@RequestPath(value = "/clearUserCache")
	public Resp clearUserCache(HttpRequest request, String nick) throws Exception {
		Integer uid = UserService.ME.getUidByNick(nick);
		if (uid == null) {
			return Resp.fail("昵称不存在");
		}
		User user = UserService.ME.getById(uid);
		if (user == null) {
			return Resp.fail("不能根据uid找到用户");
		}

		UserService.ME.clearCache(uid, user.getLoginname());

		return Resp.ok();
	}

	/**
	 * 根据loginname添加角色
	 * @param request
	 * @param loginname
	 * @param roleid
	 * @return
	 * @author tanyaowu
	 */
	@RequestPath(value = "/addRoleByLoginname")
	public Resp addRoleByLoginname(HttpRequest request, String loginname, byte roleid) {
		return UserService.ME.addRoleByLoginname(loginname, roleid);
	}

	/**
	 * 根据nick添加
	 * @param request
	 * @param nick
	 * @param roleid
	 * @return
	 * @author tanyaowu
	 */
	@RequestPath(value = "/addRoleByLNick")
	public Resp addRoleByLNick(HttpRequest request, String nick, byte roleid) {
		return UserService.ME.addRoleByNick(nick, roleid);
	}

}
